package com.haochang.chunkvod.controller.fragment.hostess;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.haochang.base.BaseActivity;
import com.haochang.base.BaseFragment;
import com.haochang.base.OnBaseClickListener;
import com.haochang.chunkvod.R;
import com.haochang.chunkvod.app.common.intent.IntentKey;
import com.haochang.chunkvod.app.utils.ToastUtils;
import com.haochang.chunkvod.model.hostess.HostessData;

/**
 *         created at 2016/8/25 16:27
 *         房间管理 - 带客 页面
 */
public class RoomWaitFragment extends BaseFragment
{
	private HostessData hostessData;
	private String roomId;

	@Nullable
	@Override
	public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
	{
		View view = inflater.inflate(R.layout.fragment_room_wait, container,false);
		initView(view);
		return view;
	}

	/**
	 * 初始化控件
	 *
	 */
	private void initView(View view)
	{
		Button room_wait_open = (Button) view.findViewById(R.id.room_wait_open);
		Button room_wait_cancel = (Button) view.findViewById(R.id.room_wait_cancel);
		room_wait_open.setOnClickListener(listener);
		room_wait_cancel.setOnClickListener(listener);
		hostessData = new HostessData((BaseActivity) getActivity());
		roomId = getArguments().getString(IntentKey.ROOM_ID);
	}

	private OnBaseClickListener listener = new OnBaseClickListener()
	{
		@Override
		public void onBaseClick(View v)
		{
			switch (v.getId())
			{
			case R.id.room_wait_open:
				//开机
				if (!TextUtils.isEmpty(roomId))
				{
					hostessData.requestOpenRoom(roomId, new HostessData.NoticeListener()
					{
						@Override
						public void onRequestSuccess(String notice)
						{
							if (!TextUtils.isEmpty(notice))
							{
								getActivity().finish();
							}
						}

						@Override
						public void onFail(int errno, String errMsg)
						{
							ToastUtils.showText(getActivity(), TextUtils.isEmpty(errMsg) ? "开机失败!" : errMsg);
						}
					});
				}
				break;
			case R.id.room_wait_cancel:
				//取消带客
				if (!TextUtils.isEmpty(roomId))
				{
					hostessData.requestCancelGuilding(roomId, new HostessData.NoticeListener()
					{
						@Override
						public void onRequestSuccess(String notice)
						{
							if (!TextUtils.isEmpty(notice))
							{
								ToastUtils.showText(getContext(), "状态修改成功");
								finishThenChangeState("using");
							}
						}

						@Override
						public void onFail(int errno, String errMsg)
						{
							ToastUtils.showText(getActivity(), TextUtils.isEmpty(errMsg) ? "取消带客失败!" : errMsg);
						}
					});
				}
				break;
			}
		}
	};

	/**
	 * 返回首页并改变状态 //先返回数据，本地先刷新，再网络刷新
	 * @param newState 新状态
	 */
	private void finishThenChangeState(String newState)
	{
		Intent intent = getActivity().getIntent();
		intent.putExtra(IntentKey.ROOM_ID, getArguments().getString(IntentKey.ROOM_ID));
		intent.putExtra(IntentKey.ROOM_STATUS, newState);
		getActivity().setResult(Activity.RESULT_OK, intent);//先返回数据，本地先刷新，再网络刷新
		getActivity().finish();
	}
}
